package cn.dankal.flyaudio.friend.utils;

import android.graphics.Bitmap;
import android.os.Environment;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import cn.dankal.basiclib.util.ToastUtils;

public class ShareUtils {
    private static ShareUtils instance;

    private Bitmap bitmap;

    private File file;

    public void setShareBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
        try {
            File file = saveFile(bitmap, "fly_share_img_"+System.currentTimeMillis()+".jpg");
            setFile(file);
        } catch (IOException e) {
            ToastUtils.showShort("保存失败");
            e.printStackTrace();
        }
    }

    public File saveFile(Bitmap bm, String fileName) throws IOException {//将Bitmap类型的图片转化成file类型，便于上传到服务器
        String path = Environment.getExternalStorageDirectory() + "/Ask";
        File dirFile = new File(path);
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }
        File myCaptureFile = new File(path + fileName);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
        return myCaptureFile;
    }

    public void setSharePicFile(File file) {
        this.file = file;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public Bitmap getShareBitmap() {
        return bitmap;
    }

    public void destory() {
        bitmap.recycle();
        bitmap = null;
    }

    public static ShareUtils getInstance() {
        if (instance == null) {
            instance = new ShareUtils();
        }
        return instance;
    }
}
